﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
using Utility;
namespace WebTeamplate
{

    public partial class Admin_SubCategoryDetail : BasePage
    {
        #region Fields
        public string urlList = "ManageSubcategory.aspx?categoryId={0}";
        public string validateGroup = "subCate";
        #endregion

        #region ViewStates
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }
        
        public int SubCategoryID
        {

            get
            {

                object o = ViewState["SubCategoryID"];

                if (o != null) return (int)o;

                return int.MinValue;

                //a default

            }

            set { ViewState["SubCategoryID"] = value; }

        }

        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
           
            if (!Page.IsPostBack)
            {
                LoadCategory();
                CommonFunction.InitStatusPublishForComboboxDetail(drlPublish);
                InitParamater();
                InitTextControl();
                LoadData();
                drlCategory.Focus();
            }
        }

        private void LoadData()
        {
            SubCategory objData = SubCategoryUtil.Get(this.SubCategoryID,drlCategory.SelectedValue);
            if (objData != null)
            {
                if(objData.IsPublished)
                        drlPublish.SelectedValue = "1";
                else drlPublish.SelectedValue = "0";
                drlCategory.SelectedValue = objData.CategoryId;
                txtName.Text = objData.SubCategoryName;
                if (drlCategory.SelectedValue != CommonFunction.GetValueFromQueryString(this.Page, "categoryid"))
                    Response.Redirect("Default.aspx");
               // CommonFunction.SetDisableAdminTextBox(txtID, true);
               
            }
        }
       
        #endregion

        #region Methos
     
        private void InitParamater()
        {
            SubCategoryID = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "subcategoryid"));
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
            
            if (this.Command == string.Empty)
                   this.Command = CommonFunction.DEFAULT_COMMAND_ADD;
            
            drlCategory.SelectedValue = CommonFunction.GetValueFromQueryString(this.Page, "categoryid");
            if (drlCategory.SelectedValue == CommonFunction.GetValueFromQueryString(this.Page, "categoryid"))
                trCate.Visible = false;
            else Response.Redirect("Default.aspx");
        }
       
        private void InitTextControl()
        {
           
            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCacel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            refvCategory.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblCategory));
            refvCategory.ValidationGroup = validateGroup;
            refvName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblName));
            refvName.ValidationGroup = validateGroup;
            refvPublish.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblPublish));
            refvPublish.ValidationGroup = validateGroup;
            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;
        }
        private void Update(SubCategory objData)
        {
            objData.SubCategoryId = this.SubCategoryID;
            if (!SubCategoryUtil.Update(objData))
                lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;
            else
                Response.Redirect(GetUrlList());

        }
        private void Insert(SubCategory objData)
        {
                       
            if (!SubCategoryUtil.Insert(objData))
                lblErrorMessage.Text = Resources.Resource.MESSAGE_INSERT_ERROR;
            
            else
                Response.Redirect(GetUrlList());

        }
        private string GetUrlList()
        {
            return string.Format(urlList, drlCategory.SelectedValue);
        }
        public void LoadCategory()
        {
           
            List<CategoryResult> lstResult = CategoryUtil.GetAllCategoryToControl();
            if (lstResult != null)
            {
                drlCategory.Items.Clear();
                drlCategory.Items.Add(new ListItem(BaseFunctions.COMBOBOX_NULL_VALUE, string.Empty));
                foreach (CategoryResult objCate in lstResult)
                {
                    drlCategory.Items.Add(new ListItem(objCate.CategoryName, objCate.CategoryId));
                }
            }
        }
       
        #endregion

        #region Events, Handler
        protected void drlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Command == CommonFunction.DEFAULT_COMMAND_EDIT)
                LoadData();
            // else
            //   ResetForm();
        }
        protected void btnCacel_Click(object sender, EventArgs e)
        {
            Response.Redirect(GetUrlList());
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.GetCurrentPageAndParamaterUrl());
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SubCategory objData = new SubCategory();
                if(drlPublish.SelectedValue=="1")
                    objData.IsPublished = true;
                else objData.IsPublished = false;
                objData.CategoryId = drlCategory.SelectedValue;
                objData.SubCategoryName = txtName.Text;
                objData.CreatedBy = objData.LastUpdatedBy = this.userLoginName;
                objData.CreatedDate = objData.LastUpdatedDate = CommonFunction.Now;
                if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
                    Insert(objData);
                else
                    Update(objData);

            }
            else
            {
                vsumCategory.ShowMessageBox = false;
                vsumCategory.ShowSummary = true;
            }
        }
        #endregion

    }
}
